package com.yuedou.jylt.activity;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.view.View;
import android.widget.Toast;

import com.yuedou.jylt.R;
import com.yuedou.jylt.constant.CameraConstants;
import com.yuedou.jylt.service.MainService;
import com.yuedou.jylt.service.MainService2;
import com.yuedou.jylt.service.MainService3;
import com.yuedou.jylt.utils.PathUtils;
import com.yuedou.jylt.utils.ShellUtils;

public class MediaActivity extends AppCompatActivity {

    private final int REQUEST_CODE_PERMISSIONS = 10;

    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media);
        initListener();
        context = this;
        PathUtils.getInstance().initPath(MediaActivity.this);
    }

    private void initListener() {
        findViewById(R.id.tv_11).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!MainService.isrun) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (Settings.canDrawOverlays(context)) {
                            Intent intent = new Intent(MediaActivity.this, MainService.class);
                            startService(intent);
                        } else {
                            Uri uri = Uri.parse("package:" + MediaActivity.this.getPackageName());
                            Intent intent3 = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, uri);
                            startActivityForResult(intent3, 100);
                        }
                    }
                }
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        MainService.getInstance().showWindow(1);
                    }
                }, 2000);
            }
        });

        findViewById(R.id.tv_12).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!MainService2.isrun) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (Settings.canDrawOverlays(context)) {
                            Intent intent = new Intent(MediaActivity.this, MainService2.class);
                            startService(intent);
                        } else {
                            Uri uri = Uri.parse("package:" + MediaActivity.this.getPackageName());
                            Intent intent3 = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, uri);
                            startActivityForResult(intent3, 100);
                        }
                    }
                }
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        MainService2.getInstance().showWindow(1);
                    }
                }, 2000);

            }
        });
        findViewById(R.id.tv_13).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!MainService3.isrun) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (Settings.canDrawOverlays(context)) {

                            Intent intent = new Intent(MediaActivity.this, MainService3.class);
                            startService(intent);
                        } else {
                            Uri uri = Uri.parse("package:" + MediaActivity.this.getPackageName());
                            Intent intent3 = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, uri);
                            startActivityForResult(intent3, 100);
                        }
                    }
                }
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        MainService3.getInstance().showWindow(1);
                    }
                }, 2000);

            }
        });

        findViewById(R.id.tv_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MainService.isrun) {
                    MainService.getInstance().closeCamera();

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (Settings.canDrawOverlays(context)) {
                            Intent intent = new Intent(MediaActivity.this, MainService.class);
                            stopService(intent);
                        } else {
                            Uri uri = Uri.parse("package:" + MediaActivity.this.getPackageName());
                            Intent intent3 = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, uri);
                            startActivityForResult(intent3, 100);
                        }
                    }

                }

                if (MainService2.isrun) {
                    MainService2.getInstance().closeCamera();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (Settings.canDrawOverlays(context)) {
                            Intent intent = new Intent(MediaActivity.this, MainService2.class);
                            stopService(intent);
                        } else {
                            Uri uri = Uri.parse("package:" + MediaActivity.this.getPackageName());
                            Intent intent3 = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, uri);
                            startActivityForResult(intent3, 100);
                        }
                    }
                }
                ShellUtils.execCommand(CameraConstants.KEY_CAMERA_RELEASE,false);
            }
        });
        findViewById(R.id.tv_3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MainService.isrun) {
                    MainService.getInstance().showWindow(1);
                }
                if (MainService2.isrun) {
                    MainService2.getInstance().showWindow(1);
                }
            }
        });

        findViewById(R.id.tv_4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MediaActivity.this,UseTimeActivity.class).putExtra("time",System.currentTimeMillis()));
            }
        });

        findViewById(R.id.tv_5).setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View v) {
                if (!MainService2.isrun) {
                    if (Settings.canDrawOverlays(context)) {
                        Intent intent = new Intent(MediaActivity.this, MainService2.class);
                        startService(intent);
                    } else {
                        Uri uri = Uri.parse("package:" + MediaActivity.this.getPackageName());
                        Intent intent3 = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, uri);
                        startActivityForResult(intent3, 100);
                    }
                }
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        MainService2.getInstance().showWindow(1);
                    }
                }, 2000);

            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            if (Build.VERSION.SDK_INT >= 23 && Settings.canDrawOverlays(this)) {
                Intent intent2 = new Intent(MediaActivity.this, MainService.class);
                startService(intent2);
            } else {
                Toast.makeText(this,"permission denied.",Toast.LENGTH_LONG).show();
            }
        }
    }
}